import time
import requests
import re
import json

def get_gt():
    url = 'https://www.geetest.com/demo/gt/register-slide'
    data = {
        't':int(time.time() * 1000)
    }
    res = requests.get(url,params=data,verify=False).json()
    print(int(time.time() * 1000))
    print(res['challenge'],res['gt'])
    return res['challenge'],res['gt']


def get_data(challenge,gt):
    time.sleep(4)
    url = 'https://api.geetest.com/get.php'
    data = {
        'is_next': 'true',
        'type': 'slide3',
        'gt': gt,
        'challenge': challenge,
        'lang': 'zh-cn',
        'https': 'true',
        'protocol': 'https://',
        'offline': 'false',
        'product': 'embed',
        'api_server': 'api.geetest.com',
        'isPC': 'true',
        'autoReset': 'true',
        'width': '100%',
        'callback': 'geetest_' + str(int(time.time() * 1000)),
    }
    headers = {
        # 'Referer': 'https:/www.geetest.com/',
        'User-Agent': 'Mozilla/5.01682558 Mozilla/5.0 (Windows; U; Windows NT 5.2; en-US) AppleWebKit/537.36(KHTML, like Gecko) Chrome/40.0.2214.89 Safari/537.36',
        'Cookie': 'GeeTestUser=f00830a4b304ebf4c78266cf3a442a83; GeeTestAjaxUser=4ad29fcf1d5a8571e0c4f736c1ff362b; Hm_lvt_25b04a5e7a64668b9b88e2711fb5f0c4=1624538157,1624538721,1627097157,1627117397; sensorsdata2015jssdkcross=%7B%22distinct_id%22%3A%2217a3e04b0b22fd-0380a77c98d151-6373267-1327104-17a3e04b0b3d71%22%2C%22%24device_id%22%3A%2217a3e04b0b22fd-0380a77c98d151-6373267-1327104-17a3e04b0b3d71%22%2C%22props%22%3A%7B%22%24latest_referrer%22%3A%22%22%2C%22%24latest_referrer_host%22%3A%22%22%2C%22%24latest_traffic_source_type%22%3A%22%E7%9B%B4%E6%8E%A5%E6%B5%81%E9%87%8F%22%2C%22%24latest_search_keyword%22%3A%22%E6%9C%AA%E5%8F%96%E5%88%B0%E5%80%BC_%E7%9B%B4%E6%8E%A5%E6%89%93%E5%BC%80%22%2C%22%24latest_landing_page%22%3A%22https%3A%2F%2Fwww.geetest.com%2Fcontact%23report%22%7D%7D; Hm_lpvt_25b04a5e7a64668b9b88e2711fb5f0c4=1627118574'
    }

    res = requests.get(url=url,params=data,headers=headers,verify=False).text
    print(data['callback'])
    datas = re.findall('{(.*?)}',res)[0]
    print(datas)

    # print(re.findall('{(.*?)}',res)[0])
    # print((json.dumps(data).decode("unicode-escape")))

if __name__ == '__main__':
    challenge, gt = get_gt()
    get_data(challenge,gt)